/*
 * Filtrar.java
 *
 * Created on 19/06/2011, 12:56:03
 */

package pbf.dialog;

import java.util.ArrayList;
import javax.swing.ButtonGroup;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Atendimento;
import pbf.util.DataHora;
import pbf.util.Mensagem;

/**
 *
 * @author Ricardo
 */
public class Filtrar extends javax.swing.JDialog {

    private ButtonGroup grupoBotao = new ButtonGroup();
    private ArrayList<Atendimento> listaAtendimento = new ArrayList<Atendimento>();
    private static Session sessao = sessao = HibernateUtil.openSession();
    private int familiaId;

    public Filtrar(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        grupoBotao.add(radioAgendamento);
        grupoBotao.add(radioAtendimento);
    }

        private void selectRadio(){
        //Desabilita os campos
        agendamentoDtAgendamento.setEditable(false);
        atendimentoDtAtendimento.setEditable(false);

        //Habilita campo selecionado
        if(radioAgendamento.isSelected())
            agendamentoDtAgendamento.setEditable(true);
        if(radioAtendimento.isSelected())
            atendimentoDtAtendimento.setEditable(true);
    }

        private String getQuery(){
        if (radioAgendamento.isSelected()) {
            return ("SELECT ATENDIMENTO.* FROM atendimento, agendamento, familia WHERE AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND AGENDAMENTO_FK_FAMILIA=FAMILIA_ID AND FAMILIA_ID='"+familiaId+"' AND AGENDAMENTO_DT_AGENDAMENTO BETWEEN '"+DataHora.converteStringData(agendamentoDtAgendamento.getText())+" 00:00:00' AND '"+DataHora.converteStringData(agendamentoDtAgendamento.getText())+" 23:59:59'");
        }
        else{
            return ("SELECT ATENDIMENTO.* FROM atendimento, agendamento, familia WHERE AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND AGENDAMENTO_FK_FAMILIA=FAMILIA_ID AND FAMILIA_ID='"+familiaId+"' AND AGENDAMENTO_DT_ATENDIMENTO BETWEEN '"+DataHora.converteStringData(atendimentoDtAtendimento.getText())+" 00:00:00' AND '"+DataHora.converteStringData(atendimentoDtAtendimento.getText())+" 23:59:59' ");
        }
    }

    public ArrayList<Atendimento> getAtendimentos(int familiaId){
        this.familiaId = familiaId;
        setVisible(true);
        return listaAtendimento;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator9 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButton2 = new javax.swing.JButton();
        atendimentoDtAtendimento = new javax.swing.JFormattedTextField();
        agendamentoDtAgendamento = new javax.swing.JFormattedTextField();
        radioAtendimento = new javax.swing.JRadioButton();
        radioAgendamento = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(null);
        getContentPane().add(jSeparator9);
        jSeparator9.setBounds(20, 40, 450, 10);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setText("Filtrar");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(20, 20, 290, 20);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel16.setText("Data do Atendimento:");
        getContentPane().add(jLabel16);
        jLabel16.setBounds(20, 80, 170, 20);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/cross-circle.png"))); // NOI18N
        jButton1.setText("Fechar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(370, 160, 100, 25);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel18.setText("Data do Agendamento:");
        getContentPane().add(jLabel18);
        jLabel18.setBounds(20, 50, 170, 20);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 150, 500, 10);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/clipboard-invoice.png"))); // NOI18N
        jButton2.setText("Flitrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(260, 160, 100, 25);

        atendimentoDtAtendimento.setEditable(false);
        try {
            atendimentoDtAtendimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        atendimentoDtAtendimento.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(atendimentoDtAtendimento);
        atendimentoDtAtendimento.setBounds(190, 80, 220, 23);

        try {
            agendamentoDtAgendamento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        agendamentoDtAgendamento.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(agendamentoDtAgendamento);
        agendamentoDtAgendamento.setBounds(190, 50, 220, 23);

        radioAtendimento.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        radioAtendimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAtendimentoActionPerformed(evt);
            }
        });
        getContentPane().add(radioAtendimento);
        radioAtendimento.setBounds(410, 80, 60, 23);

        radioAgendamento.setSelected(true);
        radioAgendamento.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        radioAgendamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAgendamentoActionPerformed(evt);
            }
        });
        getContentPane().add(radioAgendamento);
        radioAgendamento.setBounds(410, 50, 60, 21);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-496)/2, (screenSize.height-232)/2, 496, 232);
    }// </editor-fold>//GEN-END:initComponents

    private void radioAgendamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioAgendamentoActionPerformed
    selectRadio();
    }//GEN-LAST:event_radioAgendamentoActionPerformed

    private void radioAtendimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioAtendimentoActionPerformed
    selectRadio();
    }//GEN-LAST:event_radioAtendimentoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        sessao.beginTransaction();
        listaAtendimento = (ArrayList<Atendimento>) sessao.createSQLQuery(getQuery()).addEntity(Atendimento.class).list();
        sessao.getTransaction().commit();

        if(listaAtendimento.size()<=0){
            Mensagem.mensageiroAtencao("Nenhum resultado encontrado");
        }
        else{
            //sessao.close();
             dispose();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    sessao.close();
    }//GEN-LAST:event_formWindowClosing

    //System.out.print("SELECT ATENDIMENTO.* FROM atendimento, agendamento, familia WHERE AGENDAMENTO_ID=ATENDIMENTO_FK_AGENDAMENTO AND AGENDAMENTO_FK_FAMILIA=FAMILIA_ID AND FAMILIA_ID='"+familiaId+"' AND AGENDAMENTO_DT_AGENDAMENTO BETWEEN '2011-01-01' AND '"+DataHora.converteStringData(agendamentoDtAgendamento.getText())+" 23:59:59'");

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Filtrar dialog = new Filtrar(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField agendamentoDtAgendamento;
    private javax.swing.JFormattedTextField atendimentoDtAtendimento;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JRadioButton radioAgendamento;
    private javax.swing.JRadioButton radioAtendimento;
    // End of variables declaration//GEN-END:variables

}
